package com.sensologyhome_all.base;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.sensologyhome_all.R;
import com.sensologyhome_all.config.GlobalValue;
import com.sensologyhome_all.net.MyHttpUtil;
import com.sensologyhome_all.util.ActivityPageManager;
import com.sensologyhome_all.util.ActivityUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;



/**
 * 基础Activity 所有的Activity都继承此类
 */
public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {
    protected Activity activityContext; //使用作为Context对象
    public Vibrator vibrator;
    public SoundPool sp;
    public int soundID;

    @Override
    public void onClick(View v) {
        clickVibrator();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityContext = this;
        registerEventBus();
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        ActivityPageManager.getInstance().addActivity(this);
//        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (GlobalValue.IsAllowVibrator) {
            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        } else {
            vibrator = null;
        }
        if (GlobalValue.IsAllowVoice) {
            sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
            soundID = sp.load(getApplicationContext(), R.raw.control_button_sound, 1);
        } else {
            sp = null;
        }
    }

    public void clickVibrator() {
        Log.i("sss", "vibrator: " + vibrator + " sp: " + sp + " soundID: " + soundID);
        if (vibrator != null) {
            vibrator.vibrate(50);
        }
        if (sp != null) {
            sp.play(soundID, 0.8f, 0.8f, 1, 0, 1.0f);
        }
    }

    public void alarmVibrator() {
        if (vibrator != null) {
            vibrator.vibrate(500);
        }
    }




    @Override
    protected void onStop() {
        super.onStop();
        if (sp != null) {
            sp.stop(soundID);
            sp.release();
            sp = null;
        }
    }


    /**
     * 退出应用
     */
    /*public void onEventMainThread(ExitAppEvent exitApp) {
		if (exitApp.isExit() && exitApp.isLogin()) {
			ActivityUtil.startActivity(activityContext,new Intent(activityContext,LoginNewActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			finish();
		} else if (exitApp.isExit()) {
			unRegisterEventBus();//这一句必须加上，不然虽然能退出，但报很多错误
			finish();//到这里，多个activity可以关闭掉程序了 但是进程仍然存在，因此加上了下边一句话，可以杀死进程
			android.os.Process.killProcess(android.os.Process.myPid());
		}
	}

	public void onEventMainThread(ErrorEvent event) {
		int status = event.getStatus();
		String message = event.getMsg();
		if (event.getContext().equals(activityContext)) {
			LogUtils.i("error_status:" + event.getStatus());
			LogUtils.i("error_msg:" + event.getMsg());
			onFailure(status, message);
		}
	}*/


    /**
     * 触摸Activity空白处关闭软键盘(适用于总布局不是ScrollView的情况)
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        ActivityUtil.hindSoftInput(activityContext);
        return super.onTouchEvent(event);
    }

    /**
     * 统一Activity后退时的动画
     *
     * @see FragmentActivity#onBackPressed()
     */
    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.finish_enter, R.anim.finish_exit);
    }

    @Override
    protected void onDestroy() {
        MyHttpUtil.cancelCall(activityContext);
        unRegisterEventBus();
        ActivityPageManager.getInstance().removeActivity(this);
        super.onDestroy();
    }


    /**
     * 当前界面打开软键盘 跳转新的界面的时候关闭关键盘
     */
    @Override
    protected void onPause() {
        ActivityUtil.hindSoftInput(activityContext);
        super.onPause();
    }

    public void startActivity(Class<?> cls, Object obj) {
        Intent intent = new Intent(this, cls);
        if (obj != null) {
            intent.putExtra("data", (Serializable) obj);
        }
        startActivity(intent);

    }

    /**
     * 注册EventBus通信组件
     */
    protected void registerEventBus() {
        if (activityContext != null) {
            try {
                EventBus.getDefault().register(activityContext);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 取消注册EventBus通信组件
     */
    protected void unRegisterEventBus() {
        EventBus.getDefault().unregister(activityContext);
    }
}
